看一百遍美女,美女也不一定是你的。但你刷一百遍算法,知识就是你的了~~
谁能九层台,不用累土起!
题目地址
题目
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
- get(index):获取链表中第 
index 个节点的值。如果索引无效,则返回-1。 
- addAtHead(val):在链表的第一个元素之前添加一个值为 
val 的节点。插入后,新节点将成为链表的第一个节点。 
- addAtTail(val):将值为 
val 的节点追加到链表的最后一个元素。 
- addAtIndex(index,val):在链表中的第 
index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。 
- deleteAtIndex(index):如果索引 
index 有效,则删除链表中的第 index 个节点。 
示例:
1 2 3 4 5 6 7
   | MyLinkedList linkedList = new MyLinkedList(); linkedList.addAtHead(1); linkedList.addAtTail(3); linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3 linkedList.get(1);            //返回2 linkedList.deleteAtIndex(1);  //现在链表是1-> 3 linkedList.get(1);            //返回3
   | 
 
提示:
- 所有
val值都在 [1, 1000] 之内。 
- 操作次数将在 
[1, 1000] 之内。 
- 请不要使用内置的 LinkedList 库。
 
解题思路
- 我们定义一个头节点
head,以及用来存储链表长度的length 
get方法中我们先对传入的index先进行处理如果在0-length之间则处理否则返回-1,遍历链表当到第index个时返回对应节点的val 
- 至于
addAtHead和addAtTail我们可以统一放到addAtIndex中来处理 
addAtIndex函数中我们同样的需要先按题目要求对index处理,index<0时让其等于0 
- 然后判断链表有无节点,没有节点的话将传入的作为头结点否则就遍历链表到index的前一位置执行插入操作
 
- 删除其实也是类似的操作,找到
index所在节点的前置节点,将其next指向index后置节点 
解题代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
   | var MyLinkedList = function() {     this.head = null     this.length = 0 }; var NodeList = function (val,next){     this.val = val     this.next = next }
 
 
 
 
  MyLinkedList.prototype.get = function(index) {     let cur = this.head     if(index>=0&&index<this.length){         if(index==0) return this.head.val         let i = 0;         while (cur  && i < index) {             cur = cur.next;             i++;         }         return cur.val;     }     return -1 };
 
 
 
 
  MyLinkedList.prototype.addAtHead = function(val) {     this.addAtIndex(0, val); };
 
 
 
 
  MyLinkedList.prototype.addAtTail = function(val) {     this.addAtIndex(this.length, val); };
 
 
 
 
 
  MyLinkedList.prototype.addAtIndex = function(index, val) {     let cur = this.head     if(index>this.length) return     if(index<=0||this.size==0){         this.head = new NodeList(val)        this.head.next = cur     }else{         while (index-- > 1) {             cur = cur.next         }         let addNode = new NodeList(val)         addNode.next = cur.next         cur.next = addNode     }     this.length++ };
 
 
 
 
  MyLinkedList.prototype.deleteAtIndex = function(index) {     if(!this.head) return     if(index>=0&&index<this.length){         let cur = this.head        if(index==0){            this.head = cur.next        }else {            let pre =null            let i=0            while (i<index && cur){                pre = cur                cur = cur.next                i++            }            pre.next = cur.next        }         this.length--     } };
  | 
 
如有任何问题或建议,欢迎留言讨论!